package com.fragment;

import interfaces.NavigateToListLocate;
import interfaces.NavigateToLocateDetail;

import java.util.ArrayList;

import util.ApplicationConstant;

import com.activity.MainAppActivity;
import com.activity.R;
import com.activity.R.id;
import com.activity.R.layout;
import com.activity.R.string;

import dao.LoadCategory;
import dao.LoadLocate;
import adapter.AdapterLocate;
import android.app.ActionBar;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;

public class ListLocateFragment extends Fragment implements
		NavigateToListLocate{
	public MainAppActivity mainAppActivity;
	public static Boolean isLoadingList = false;// bien k onscrooll loading
												// nhieu lan
	public static String caId;// caid hien tai
	public static String caName;// ca name hien tai
	public static AdapterLocate adapterLocate;
	public static int DISTANCE[];
	// view------------------
	public static ProgressBar progressBar;
	public static ListView listView;
	// public static ProgressDialog progressDialog;
	public static LinearLayout linearLayoutLoading;
	public static TextView txtTotalResults;
	public static LinearLayout notFoundLayout;
	// resoure string-----------------------------------------
	public static String RESULTS = "";
	public static String RESULT = "";
	public static String LOADING = "";
	public static String TITLE_SHOW_ON_MAP = "";
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		this.mainAppActivity = (MainAppActivity) activity;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		ApplicationConstant.IS_CALLING_BOOKMARK=false;
		ApplicationConstant.IS_CALLING_RECENT=false;
		MainAppActivity.TAG_FRAGMENT = "ListLocateFragment";
		View rootView = inflater
				.inflate(R.layout.list_locate, container, false);
		// get resoure
		RESULTS = getResources().getString(R.string.results);
		RESULT = getResources().getString(R.string.result);
		LOADING = getResources().getString(R.string.Loading_more);
		TITLE_SHOW_ON_MAP = getResources().getString(R.string.show_on_map);
		// find view by id
		linearLayoutLoading = (LinearLayout) rootView
				.findViewById(R.id.layout_locate_loading);
		notFoundLayout = (LinearLayout) rootView
				.findViewById(R.id.not_found_layout);
		listView = (ListView) rootView.findViewById(R.id.list_locate);
		txtTotalResults = (TextView) rootView
				.findViewById(R.id.txt_total_result);
		txtTotalResults.setTypeface(MainAppActivity.typeFaceFont);
		if (LoadLocate.listLocate.size() > 0)
			txtTotalResults.setText("" + LoadLocate.listLocate.size() + " "
					+ RESULTS);
		else
			txtTotalResults.setText("" + LoadLocate.listLocate.size() + " "
					+ RESULT);

		progressBar = new ProgressBar(getActivity());
		listView.addFooterView(progressBar);
		//load list locate, khong load lai khi back
		if (LoadLocate.listLocate.size() == 0) {
			LoadLocate.listLocate.clear();
			LoadLocate loadLocate = new LoadLocate(
					MainAppActivity.distanceSelected, caName);
			loadLocate.execute();
		}

		adapterLocate = new AdapterLocate(getActivity(),
				R.layout.listview_locate_item, LoadLocate.listLocate);
		listView.setAdapter(adapterLocate);
	
		listView.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				if (isLoadingList == false) {
					if ((totalItemCount - 1) > 0
							&& (totalItemCount - 1)
									% ApplicationConstant.limitItem == 0
							&& (firstVisibleItem + visibleItemCount) == totalItemCount) {
						Log.d("ListLocateFragment Loading more item...",
								"----------------//////////");
						Log.d("---ListLocateFragment loadding mor item list locate size:",
								LoadLocate.listLocate.size() + "");
						LoadLocate loadLocate = new LoadLocate(
								MainAppActivity.distanceSelected, caName);
						loadLocate.setCalledFromScroll(true);
						loadLocate.execute();
						isLoadingList = true;
					} else {
						progressBar.setVisibility(View.GONE);
					}
				}
			}
		});

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				// TODO Auto-generated method stub
				mainAppActivity.navigateToLocateDetail(pos);
			}
		});
		return rootView;
	}

	@Override
	public void navigateToListLocate(String caId, String caName) {
		// TODO Auto-generated method stub
		this.caId = caId;
		this.caName = caName;
	}

	public void updateList() {
		adapterLocate.notifyDataSetChanged();
	}

}
